﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OnlineStore.Data_Access_Layer;
using System.Data.Objects;
using System.Windows.Forms;


namespace OnlineStore.Business_Logic_Layer
{
    class BizExport : OnlineStore.BisTrans
    {
        // check export qty is number
        public bool ValidateData(DataGridViewRowCollection OrderDetail)
        {
            try
            {
                for (int i = 0; i < OrderDetail.Count; i++)
                    int.Parse(OrderDetail[i].Cells[0].Value.ToString());                    
            }
            catch (Exception ex)
            {
                if (ex.Message == "Input string was not in a correct format.")                    
                    return false;
            }
            return true;
        }
        // Check stock on store.
        public bool CheckStockOnStore(DataGridViewRowCollection OrderDetail)
        {
            BizProduct eBizProduct = new BizProduct();
            tbProduct eProduct = new tbProduct();
            int nExportQty = 0;

            try
            {
                for (int i = 0; i < OrderDetail.Count; i++)
                {
                    eProduct = eBizProduct.GetProductbyKeyWord("ID",OrderDetail[i].Cells[2].Value.ToString()).First();
                    nExportQty = int.Parse(OrderDetail[i].Cells[0].Value.ToString());
                    if (nExportQty > eProduct.Stock_Qty)
                        return false;
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }

            return true;
        }
        public int SaveExport(int nOrderID, DataGridViewRowCollection OrderDetail, string strTranDate)
        {             
            try
            {
                int nResult = 0;                
                BizProduct eBizProduct = new BizProduct();
                for (int i = 0; i < OrderDetail.Count; i++)
                {
                    // Update stock on store
                    eBizProduct.UpdateOrdertoStockProduct(int.Parse(OrderDetail[i].Cells[2].Value.ToString()),int.Parse(OrderDetail[i].Cells[1].Value.ToString()), int.Parse(OrderDetail[i].Cells[0].Value.ToString()));
                    // Save in to transaction
                    nResult += SaveTransaction(strTranDate, OrderDetail[i].Cells[2].Value.ToString(), 0-int.Parse(OrderDetail[i].Cells[0].Value.ToString()), nOrderID.ToString());
                }
                return nResult;

            }
            catch (Exception ex)
            {
                ex.ToString();
                return -1;
            }
            return -1;
        }
    
    }
}
